// packagePersonalCenter/pages/rollout2/rollout2.js
Page({

  /**
   * 页面的初始数据
   */
    data: {
        i:0,
        inputValue:'',
        balance:13.14,
        showBalance:false,
        showPasswordInputFlag: false,
        maskClass: 'mask',
        passwordInputClass: 'password-input-container',
        password: '',
        correctPassword: '123456', // 假设正确的支付密码是123456
        passwordArray: ['', '', '', '', '', ''], // 用于显示数字小方框的数组
        currentFocusIndex: 0, // 当前聚焦的输入框索引
        NumberList:[
            {
                NumberId:1,
                NumberContent:164387888
            },
            {
                NumberId:2,
                NumberContent:188888661
            }
        ],
        selectedNumber:{
            NumberId:null,
            NumberContent:null
        },
        userId:'',
        token:''
      },
      selectNumber:function(e){
          const Number=e.currentTarget.dataset.number;
        this.setData({
            selectedNumber:Number
        })
      },
      deleteNumber:function(e){
          const numberId=e.currentTarget.dataset.numberId;
          let newNumberList = this.data.NumberList.filter(item => item.NumberId!== numberId);
        this.setData({
            NumberList:newNumberList,
            selectedNumber:{
                NumberId:null,
                NumberContent:null
            }
        })
      },
      showBalance(){
        this.setData({
            showBalance:!this.data.showBalance,
        });
        this.updateInputValue();
      },
      changeIndex(e){
        this.setData({
            i: e.target.dataset.i
        })
        console.log('i=',this.data.i);
    },
    updateInputValue: function () {
        let inputValue = null;
        if (this.data.showBalance) {
          inputValue = this.data.balance;
        } else {
          inputValue = null;
        }
        this.setData({
          inputValue: inputValue
        });
      },
      //输入金额
      onInputChange:function(e){
          this.setData({
              inputValue:e.detail.value
          });
      },
      transferMoney:function(){
        const transferAmount = parseFloat(this.data.inputValue);
        const balance = this.data.balance;
        
        if (isNaN(transferAmount)) {
            wx.showToast({
                title: '请输入正确的金额',
                icon: 'none'
            });
            return;
        }
        
        if (transferAmount > balance) {
            wx.showToast({
                title: '金额不正确',
                icon: 'error'
            });
            return;
        }
        // 金额验证通过，弹出密码框
        this.showPasswordInput();   
    },
  // 点击确认转出按钮，显示支付密码输入框和遮罩层，并添加动态效果类名
  showPasswordInput: function () {
    this.setData({
      showPasswordInputFlag: true,
      maskClass: 'mask show',
      passwordInputClass: 'password-input-container show'
    });
   },
   // 隐藏支付密码输入框和遮罩层
   hidePasswordInput: function () {
    this.setData({
      showPasswordInputFlag: false,
      maskClass: 'mask',
      passwordInputClass: 'password-input-container'
    });
   },
   // 输入单个数字
   inputDigit: function (e) {
    let index = e.currentTarget.dataset.index;
    let passwordArray = this.data.passwordArray;
    passwordArray[index] = e.detail.value;
    let password = passwordArray.join('');
    let newFocusIndex = index < 5? index + 1 : index;
    this.setData({
      password: password,
      passwordArray: passwordArray,
      currentFocusIndex: newFocusIndex
    });
   },
   // 处理输入框失去焦点事件
   handleBlur: function (e) {
    let index = e.currentTarget.dataset.index;
    let passwordArray = this.data.passwordArray;
    if (passwordArray[index] === '') {
      // 如果当前输入框为空，将焦点设置到前一个输入框
      let newFocusIndex = index > 0? index - 1 : 0;
      this.setData({
        currentFocusIndex: newFocusIndex
      });
    }
   },
   // 确认支付密码
   confirmPassword: function () {
    wx.request({
      url: 'http://localhost:8080/system/wallet/transaction', // 这里替换为你要请求的实际 API 地址
      method: 'PUT', // 请求方法，可以是 'POST'、'PUT'、'DELETE' 等，根据实际需求修改
       header: {
                  'authorization': 'Bearer\u0020' + this.data.token
              },
      data:{
        senderId:this.data.userId,//发钱人,用户id
        phone:this.data.selectedNumber.NumberContent,//手机号
        money:this.data.inputValue,//金额
        payPassword:this.data.password,//支付密码
        act:1,//转入或者转出
      },
      success: (res) => {
        // 请求成功的回调函数
        console.log(res);
    
          this.setData({
            showPasswordInputFlag: false,
            maskClass: 'mask',
            passwordInputClass: 'password-input-container'
          });
        if (res.data.code ==200) {
          console.log("成功支付");
          wx.navigateTo({
            url: "/packagePersonalCenter/pages/orderPaymentSuccess/orderPaymentSuccess?param="+this.data.inputValue
          })
          return;
        }
        console.log("支付失败");
        wx.showModal({
          content: '支付失败',
          showCancel: false,
          confirmText: '确定',  
        });
      },
      fail: (err) => {
        // 请求失败的回调函数
        console.error('请求失败:', err);
        this.setData({
          responseData: '请求失败，请稍后重试'
        });
      }
    });
  },
  // 忘记密码
  forgetPassword: function () {
    wx.navigateTo({
        url: '/packagePersonalCenter/pages/clpwdverify/clpwdverify',
      });
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app=getApp()
     const token = wx.getStorageSync('token');//获取令牌
    this.setData({
      userId:app.globalData.userInfo.userId,
      token:token
    })
    this.updateInputValue();
    this.getBalance();
  },
  getBalance(){
    wx.request({
      url: 'http://localhost:8080/system/wallet/'+this.data.userId, // 这里替换为你要请求的实际 API 地址
      method: 'GET', // 请求方法，可以是 'POST'、'PUT'、'DELETE' 等，根据实际需求修改
       header: {
                  'authorization': 'Bearer\u0020' + this.data.token
              },
      success: (res) => {
        // 请求成功的回调函数
        this.setData({
          balance: res.data.data.balance
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})